package com.wisdytech.common.websocket.handler;

import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import com.wisdytech.linkdcs.system.model.SysUser;

/**
 * 创建握手 获取用户信息兵交由websocket管理
 * @author dlb
 *
 */
public class WebSocketInterceptor implements HandshakeInterceptor{
	
	private static Logger logger =LoggerFactory.getLogger(WebSocketInterceptor.class);

	@Override
	public void afterHandshake(ServerHttpRequest arg0, ServerHttpResponse arg1, WebSocketHandler arg2, Exception arg3) {
		
	}

	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse arg1, WebSocketHandler arg2,
			Map<String, Object> attributes) throws Exception {
		logger.info("用户建立websocket连接。。。");
        if (request instanceof ServletServerHttpRequest) {
        	ServletServerHttpRequest httpRequest=(ServletServerHttpRequest) request;
        	
	        //组态Id
	        String zutaiId = httpRequest.getServletRequest().getParameter("id");
	        //时间戳，确保唯一
	        String time = httpRequest.getServletRequest().getParameter("time");
	        
//	        SysUser sysUser =(SysUser)SecurityUtils.getSubject().getPrincipal();
	        
	        //写入组态id和用户信息
	        attributes.put("zutaiId", zutaiId);
	        attributes.put("time", time);
           
        }

        return true;
	}

}
